package com.jz.banner.screen.image;

import android.content.Context;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.GlideModule;
import com.jz.banner.screen.utils.CommUtils;

public class MyGlideModule implements GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // 设置磁盘缓存为512M，缓存在内部缓存目录
        long cacheSize100MegaBytes = 512 * 1024 * 1024L;
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
        //builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));

        // 20%大的内存缓存作为 Glide 的默认值
        MemorySizeCalculator.Builder memorySizeCalculatorBuilder = new MemorySizeCalculator.Builder(context);
        int defaultMemoryCacheSize = memorySizeCalculatorBuilder.build().getMemoryCacheSize();//20m
        int defaultBitmapPoolSize = memorySizeCalculatorBuilder.build().getBitmapPoolSize();//10m

        CommUtils.Companion.println("MyGlideModule->", "defaultMemoryCacheSize:" + defaultMemoryCacheSize + ",defaultBitmapPoolSize:" + defaultBitmapPoolSize);
        int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
        int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);

        builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
        builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));

    }


    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {

    }
}
